define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {  
                   loading:0,
                    page:1,
                    status:0,
                    lastpage:0,
                    listData:[],
                    orderPrintloading:{},
                    orderPrintdisabled:{},
                    popup3:0,
                    popup:0,
                    popup2:0,
                    remark:'',
                    nodata:0,
                    refund_money:0
                };
            },
            el: '#pageOrder',
            created: function() {
                this.fetchData();
            },
            mounted: function() {
                    // console.log("123");
                    if (this.mounted == 1) {
                        return;
                    }
                    
                    var that = this ;
                    app.pager("#pageOrder",function(){
                       that.loadOderList();
                    });
                    this.mounted = 1;

            },
            methods: {
                fetchData: function() {
                    if (this.$route.params.status){
                      this.status = this.$route.params.status;
                    }
                    this.loadOderList();
                },

                setStatus:function(status){
                    // var status = $(e.target).attr("data-status");

                    if (this.status === status){
                      return ;
                    }
                      console.log(status);
                    this.status  = status;
                     
                    this.loadOderList(1);
                   
                },  
                topay:function(e){

                },

              loadOderList:function(reload){
                var that = this;
               
                if (reload == 1) {
                    this.loading= 0;
                    this.page=1;
                    this.lastpage= 0;
                    
                    this.nodata=0;
                }

                var page = this.page;
                if (this.loading == 1 || this.lastpage == 1) {
                  return;
                }

                this.loading = 1;
                app.get("apiv2/order/get", {
                  page: page,
                  limit: 10,
                  status: this.status,
                  order: 'addtime desc'
                }, function (res) {
                  that.loading = 0;
                  if (res.code != 200) {
                    app.alert(res.msg);
                    return;
                  }
                  // console.log(res);
                  if (res.data.list.length === 0) {
                    that.lastpage = 1;
                  }
                  that.page++;
                  if(reload==1){
                    that.listData = res.data.list;
                  }else{
                     var listData = that.listData;
                     that.listData=listData.concat(res.data.list);
                  }
                 
                  if(that.listData.length===0){
                     that.nodata=1;
                  }else{
                      that.nodata=0;
                  }
                  
                });

              },

              pageShow:function(){
                   var reload= app.getPageData("oms/order",'reload');
                   console.log(reload);
                   if(reload){
                     app.setPageData("oms/order",{reload:0});
                     this.loadOderList(1);
                   }
                  
                },

              // 取消定
              cancel_order:function(){
                if(this.order_type=="shopping"){
                  this.shopping_cancel();
                }else if(this.order_type=="book"){
                  this.book_cancel();
                }else if(this.order_type=="eat_in_hall"){
                  this.eat_in_hall_cancel();
                }
              },
             show_refund_money: function (e) {
              var order_sn = $(e.target).attr("order_sn");
              var refund_money = $(e.target).attr("refund_money");
              var paystatus = $(e.target).attr("paystatus");
              var order_type = $(e.target).attr("order_type");
              this.setData({
                  order_sn: order_sn,
                  refund_money: refund_money,
                  order_type:order_type
               });
              if(refund_money>0 && paystatus ==1){
                this.setData({
                  popup3: 1
               });
              }else{
                this.cancel_order();
              }
              
            },
             hide_refund_money:function(){
              this.popup3=0;
            },
              
       

              //确认接单
              book_receiving(order_sn){
                
                var that = this;
                app.get('/mall/dining_table_book/receiving', {
                        order_sn: order_sn
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOderList(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },
              //取消
              book_cancel(order_sn){
                
                var that = this;
                app.get('/mall/dining_table_book/cancel', {
                        order_sn: order_sn
                   
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOderList(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },
               //取消
              book_complete(order_sn){
                
                var that = this;
                app.get('/mall/dining_table_book/complete', {
                        order_sn: order_sn
                   
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOderList(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },
              
// 取消订单
              shopping_cancel(){
                var that = this;
                app.get('/mall/order/cancel', {
                        order_sn: this.order_sn,
                        refund_desc: this.remark,
                        refund_money: this.refund_money
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOderList(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },


              //确认接单
              shopping_receiving(order_sn){
              
                var that = this;
                app.get('/mall/order/receiving', {
                        order_sn: order_sn
                    }, function(data) {
                        if (data.code == 200) {
                           app.alert('操作成功');
                          that.loadOderList(1);
                        } else {
                            app.alert(data.msg);
                        }
                    });
              },

              //配送完成
              shopping_complete(order_sn) {
                var that = this;
                app.get('/mall/order/complete', {
                  order_sn: order_sn
                }, function (data) {
                  if (data.code == 200) {
                    
                    app.alert('操作成功');
                    that.loadOderList(1);
                  } else {
                    app.alert(data.msg);
                  }
                });
              },

                     //选择配送
              deliverMallOrder:function(e){
                  var that = this;
                  this.shop_id = $(e.target).attr("shop_id");
                  this.order_sn = $(e.target).attr("order_sn");
                  app.handle([
                      {
                        text:'自营配送',
                        methods:function(){
                          that.popup=1;
                        }
                      },
                      {
                        text:'蜂鸟配送',
                        methods:function(){
                          that.popup2=1;
                        }
                      },
                  ]);
              },

              //自己配送
               deliver_diy:function(e){
 
                var that = this;
                that.setData({
                  loading:1,
                  disabled:1
                });
                app.post('mall/order/delivery_diy', {
                  order_sn: this.order_sn,
                  remark: $("#formSubmit").find("[name='remark']").val(),
                  tracking_number: $("#formSubmit").find("[name='tracking_number']").val(),
                  method: 'POST',
                }, function (data) {
                  if (data.code == 200) {
                    app.alert('操作成功');
                    that.popup=0;
                    that.loadOderList(1);
                  } else {
                    app.alert(data.msg);
                    that.setData({
                      loading: 0,
                      disabled: 0
                    });
                  }
                });
              },

              //蜂鸟配送
              deliver: function (e) {
                // remark: $("#formSubmit").find("[name='remark']").val(),
                //   tracking_number: $("#formSubmit").find("[name='tracking_number']").val(),
                //   shop_id: this.shop_id,
                var that = this;
                that.setData({
                  loading: 1,
                  disabled: 1
                });
                var url = 'mall/order/delivery_dada';
                if (this.data.type == 'ele') {
                   url = 'mall/order/delivery_ele';
                }
                app.post(url, {
                  order_sn: this.data.order_sn,
                  remark: $("#formSubmit2").find("[name='remark']").val(),
                  order_weight: $("#formSubmit2").find("[name='order_weight']").val(),
                  method: 'POST',
                }, function (data) {
                  if (data.code == 200) {
                    app.alert('操作成功');
                    that.popup2=0;
                    that.loadOderList(1);
                  } else {
                    app.alert(data.msg);
                    that.setData({
                      loading: 0,
                      disabled: 0
                    });
                  }
                });
              },

              //打印小票
              orderPrint(e){
                var order_sn = $(e.target).attr("order_sn");
                var shop_id = $(e.target).attr("shop_id");
                var that = this;
                var tmp ={};
                tmp[order_sn] =1;  
                this.setData({
                  orderPrintloading: tmp ,
                  orderPrintdisabled: tmp
                });
                app.get('mall-admin/mall-order/orderPrint', {
                  order_sn: order_sn,
                  shop_id: shop_id
                }, function (data) {
                  tmp[order_sn]=0;
                  that.setData({
                    orderPrintloading: tmp,
                    orderPrintdisabled: tmp
                  });
                  if (data.error === 0) {
                    app.alert('打印指令发送成功');
                  } else {
                    app.alert(data.msg);
                  }
                });
              },

               //出餐中
            eat_in_hall_receiving(order_sn) {
              // var order_sn = $(e.target).attr("order_sn");
              // var shop_id = $(e.target).attr("shop_id");
              var that = this;
              app.get('mall/eat_in_hall_order/receiving', {
                order_sn: order_sn
              }, function (data) {
                if (data.code ==200) {
                 app.alert("操作成功");
                  that.loadOderList(1);
                } else {
                  app.alert(data.msg);
                }
              });
            },
             //出餐完成
            eat_in_hall_complete(order_sn) {
              
              var that = this;
              app.get('mall/eat_in_hall_order/complete', {
                order_sn: order_sn
              }, function (data) {
                if (data.code ==200) {
                  app.alert('操作成功');
                  that.loadOderList(1);
                } else {
                  app.alert(data.msg);
                }
              });
            },

            //取消 退款
               eat_in_hall_cancel: function () {
                var that = this;
                that.setData({
                  loading: 1,
                  disabled: 1
                });
                // if (!this.remark){
                //   app.alert('请输入备注!');
                //   return ;
                // }
                // if (!this.refund_money ) {
                //   app.alert('请输入退回金额!');
                //   return;
                // }
                app.get('mall/eat_in_hall_order/cancel', {
                  order_sn: this.order_sn,
                  refund_desc: this.remark,
                  refund_money: this.refund_money
                //  method: 'POST',
                }, function (data) {
                  if (data.code == 200) {
                    app.alert('操作成功');
                    that.hide_refund_money();
                    that.loadOderList(1);
                  } else {
                    app.alert(data.msg);
                    that.setData({
                      loading: 0,
                      disabled: 0
                    });
                  }
                });
              },



            setData:function(arr){
                  for( var i in arr){
                     this[i] = arr[i];
                  }
             },

           
           
 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
